package com.company.project.util;

import com.company.project.exception.ServiceException;
import com.company.project.model.News;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

/*
 * @Description:    综合工具类
 * @Author:         brilliantzhao
 * @CreateDate:     2019/5/30 16:18
 * @UpdateUser:
 * @UpdateDate:     2019/5/30 16:18
 * @UpdateRemark:
 */
public class Utils {

    /*
     * @Description: 只有当前登录用户有操作权限
     * @Author: brilliantzhao
     * @CreateDate: 2019/5/30 16:17
     * @Param:
     * @Return:
     */
    public static void checkOwner(JdbcTemplate jdbcTemplate, Integer id, Integer userId) {
        String sql = "SELECT * FROM news WHERE id=?";
        News news = (News) jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper(News.class));
        if (!news.getAuthorId().equals(userId)) {
            throw new ServiceException("您没有权限进行该操作");
        }
    }

}
